package com.controller;

import com.bean.LoginCreds;
import com.bean.WebSiteInfo;
import com.bean.base.WebSite;
import com.bean.base.WebSiteItem;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.LoginCredsService;
import com.service.impl.WebSiteInfoService;
import com.utils.ResUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("api/website_info")
@Slf4j
public class WebSiteInfoController {

    @Autowired
    private WebSiteInfoService webSiteInfoService;

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody WebSiteInfo webSiteInfo){
        webSiteInfoService.insert(webSiteInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody WebSiteInfo webSiteInfo){
        webSiteInfoService.update(webSiteInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        WebSiteInfo webSiteInfo=new WebSiteInfo();
        webSiteInfo.setId(id);
        webSiteInfoService.delete(webSiteInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        WebSiteInfo webSiteInfo=webSiteInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(webSiteInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<WebSiteInfo> requestData){
        WebSiteInfo webSiteInfo=requestData.getObj();
        webSiteInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        List<WebSiteInfo> pageInfo=webSiteInfoService.select(webSiteInfo,metaInfo);
        //metaInfo.setPages(pageInfo.getPages());
        //metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo,metaInfo));
    }

    @Autowired
    private  ResUtil resUtil;

    //===========================新的网址管理========================================
    @PostMapping("addWebSite")
    public ResponseEntity<ResponseData> addWebSite(@RequestBody WebSite webSite){
        String message = webSiteInfoService.addWebSite(webSite);
        return ResponseEntity.ok(resUtil.returnByMessage(message));
    }

    @DeleteMapping("delWebSite/{id}")
    public ResponseEntity<ResponseData> delWebSite(@PathVariable String id){
        WebSite webSite=new WebSite();
        webSite.setId(id);
        webSiteInfoService.delWebSite(webSite);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("getWebSite")
    public ResponseEntity<ResponseData> getWebSite(@RequestParam("id")String id){
        WebSite webSite=webSiteInfoService.getWebSite(id);
        return ResponseEntity.ok(ResponseData.success(webSite));
    }

    @PostMapping("listWebSite")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listWebSite(@RequestBody RequestData<WebSite> requestData){
        WebSite webSite=requestData.getObj();
        webSite.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        List<WebSite> pageInfo=webSiteInfoService.selectWebSite(webSite);
        //metaInfo.setPages(pageInfo.getPages());
        //metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo,metaInfo));
    }

    //===============================

    @PostMapping("addWebSiteItem")
    public ResponseEntity<ResponseData> addWebSiteItem(@RequestBody WebSiteItem webSiteItem){
        webSiteInfoService.addWebSiteItem(webSiteItem);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @DeleteMapping("delWebSiteItem/{id}")
    public ResponseEntity<ResponseData> delWebSiteItem(@PathVariable String id){
        WebSiteItem webSiteItem=new WebSiteItem();
        webSiteItem.setId(id);
        webSiteInfoService.delWebSiteItem(webSiteItem);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("getWebSiteItem")
    public ResponseEntity<ResponseData> getWebSiteItem(@RequestParam("id")String id){
        WebSiteItem webSiteItem=webSiteInfoService.getWebSiteItem(id);
        return ResponseEntity.ok(ResponseData.success(webSiteItem));
    }

    @PostMapping("listWebSiteItem")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listWebSiteItem(@RequestBody RequestData<WebSiteItem> requestData){
        WebSiteItem webSiteItem=requestData.getObj();
        webSiteItem.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        List<WebSiteItem> pageInfo=webSiteInfoService.selectWebSiteItem(webSiteItem);
        //metaInfo.setPages(pageInfo.getPages());
        //metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo,metaInfo));
    }

    @PostMapping("listWebSiteShow")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listWebSiteShow(@RequestBody RequestData<WebSiteInfo> requestData){
        WebSiteInfo webSiteInfo=requestData.getObj();
        webSiteInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        List<WebSiteInfo> pageInfo=webSiteInfoService.select(webSiteInfo,metaInfo);
        //metaInfo.setPages(pageInfo.getPages());
        //metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo,metaInfo));
    }

}
